np<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入学生邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信群名" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入微信群名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="学号" align="center" prop="studentNo" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="学生性别" align="center" prop="sex" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="学生邮箱" align="center" prop="email" />
      <el-table-column label="微信群名" align="center" prop="groupName" />
      <el-table-column label="服务类型" align="center" prop="serviceType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_service_type" :value="scope.row.serviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:student:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="uploadJob(scope.row)"
            v-hasPermi="['system:student:remove']"
          >上传作业</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="form.studentNo" placeholder="请输入学号" onKeyUp="value=value.replace(/[\W]/g,'')" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户性别" style="width: 380px;">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入学生邮箱" />
        </el-form-item>
        <el-form-item label="微信群名" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入微信群名" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-select v-model="form.serviceType"  placeholder="请选择服务类型" style="width: 380px;" :popper-append-to-body="false">
            <el-option
              v-for="dict in dict.type.sys_service_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 学生上传课程作业对话框 -->
    <el-dialog :title="title" :visible.sync="openStudentJob" width="30%">
      <el-form ref="jobForm" :model="jobForm" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="jobForm.id" v-if="false"/>
          <el-input v-model="jobForm.relationId" v-if="false"/>
          <el-input v-model="jobForm.studentNo" disabled placeholder="请输入学号" onKeyUp="value=value.replace(/[\W]/g,'')" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="jobForm.studentName" disabled placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="课程" prop="courseName">
          <el-input v-model="jobForm.courseName"disabled  placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程" prop="courseId" disabled v-if="false">
          <el-input v-model="jobForm.courseId" />
        </el-form-item>
        <el-form-item label="老师姓名" prop="studentName" >
          <el-select v-model="jobForm.teacherId"  style="width: 475px" placeholder="请选择课程信息" :popper-append-to-body="false">
            <el-option
              v-for="item in teacherList"
              :key="item.id"
              :label="item.teacherName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生邮箱" prop="email">
          <el-input v-model="jobForm.email" disabled placeholder="请输入学生邮箱" />
        </el-form-item>
        <el-form-item label="微信群名" prop="groupName">
          <el-input v-model="jobForm.groupName" disabled placeholder="请输入微信群名" />
        </el-form-item>
        <el-form-item label="布置作业"  prop="assignmentFile" label-width="120px;" v-if="null !=jobForm.assignmentFileName && '' != jobForm.assignmentFileName">
          <a :href="jobForm.assignmentFile" target="_blank" class="buttonText">{{jobForm.assignmentFileName}}</a>
        </el-form-item>
        <el-form-item label="上传作业附件" prop="contractFile">
          <file-upload v-model="jobForm.courseJobFile"  :fileType="['doc', 'xls', 'ppt', 'pdf','png', 'jpg', 'jpeg']"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateJobForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent,getStudentCourse,saveStudentJob } from "@/api/system/student";

export default {
  name: "Student",
  dicts:['sys_service_type','sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息表格数据
      studentList: [],
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openStudentJob:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentNo: null,
        studentName: null,
        sex: null,
        email: null,
        groupName: null,
        serviceType: null,
      },
      // 表单参数
      form: {},
      jobForm:{},
      // 表单校验
      rules: {
        studentNo  : [
          { required: true, message: '请输入学号', trigger: 'blur' },
          { min: 1, max: 20, message: '学号长度必须介于 1 和 20 之间', trigger: 'blur'}
        ],
        studentName : [
          { required: true, message: '请输入学生名称', trigger: 'blur' },
          { min: 1, max: 20, message: '学生名称长度必须介于 1 和 20 之间', trigger: 'blur' }
        ],
        sex : [
          { required: true, message: '请输选择学生性别', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        groupName : [
          { required: true, message: '请输入微信群名', trigger: 'blur' },
          { min: 1, max: 30, message: '微信群名长度必须介于 1 和 30 之间', trigger: 'blur' }
        ],
        serviceType : [
          { required: true, message: '请选择服务类型', trigger: 'blur' }
        ]
      },
      teacherList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询信息列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openStudentJob = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentNo: null,
        studentName: null,
        sex: null,
        email: null,
        groupName: null,
        serviceType: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        courseJobFile:null,
        relationId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息";
      });
    },
    uploadJob(row){
      this.reset();
      const id = row.id || this.ids
      getStudentCourse(id).then(response => {
        console.log(response.data[0]);
        this.jobForm = response.data[0];
        this.teacherList = response.data;
        this.openStudentJob = true;
        this.title = "上传课程作业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    updateJobForm(){
      this.$refs["jobForm"].validate(valid => {
        if (valid) {
          saveStudentJob(this.jobForm).then(response => {
            this.$modal.msgSuccess("保存成功");
            this.openStudentJob = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除信息编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
